package net.lcheng.shop.Controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import net.lcheng.model.BaseAreas;
import net.lcheng.model.User;
import net.lcheng.model.UserBankcard;
import net.lcheng.service.BaseAreasService;
import net.lcheng.service.UserBankcardService;
import net.lcheng.service.UserService;
import net.lcheng.shop.vo.CurrentUserModel;
import net.lcheng.vo.UserBankcardModel;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by zwr on 2016/7/21 0021.
 */
@Controller
public class UserBankcardController {
    /**
     * 银行卡管理
     */
    @Autowired
    private UserBankcardService userBankcardService;
    @Autowired
    private UserService userService;
    @Autowired
    private BaseAreasService baseAreasService;
    @RequestMapping(value = "/user/bankcard", method = RequestMethod.GET)
    public String userBankcardGet(Model model) {
        Subject currentUser = SecurityUtils.getSubject();
        if (currentUser.isAuthenticated()) {
            //获取当前用户
            Session session = currentUser.getSession();
           CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
            String userId = currentUserModel.getUserId();
            User user = userService.getUserByUserId(userId);
            //获取银行卡列表
            List<UserBankcard> userBankcardList = userBankcardService.getBankcardByUserId(user.getUserId());
            //把银行卡号码用****表示
            for (int i = 0; i < userBankcardList.size(); i++) {
                String userCardNo = userBankcardList.get(i).getCardNo();
                if (userCardNo!=null&&userCardNo.length() == 16) {
                    String cardNo = userCardNo.substring(0, userCardNo.length() - (userCardNo.substring(0)).length()) + "***** **** ****" + userCardNo.substring(12);
                    userBankcardList.get(i).setCardNo(cardNo);
                }
                //把银行卡号码用****表示
                if (userCardNo!=null&&userCardNo.length() == 19) {
                    String cardNo = userCardNo.substring(0, userCardNo.length() - (userCardNo.substring(0)).length()) + "***** **** ****" + userCardNo.substring(15);
                    userBankcardList.get(i).setCardNo(cardNo);
                }
            }
            model.addAttribute("userBankcardList",userBankcardList);
        }
        return "my/user_bankcard";
    }
    /**添加银行卡get*/
    @RequestMapping(value = "/user/add_bankcard", method = RequestMethod.GET)
    public String addUserBankcardGet(Model model) {
        Subject currentUser = SecurityUtils.getSubject();
        if (currentUser.isAuthenticated()) {
            //获取当前用户
            Session session = currentUser.getSession();
           CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
            String userId = currentUserModel.getUserId();
            model.addAttribute("UserBankcardModel",new UserBankcardModel());
        }else{
            return "redirect:/login";
        }
        return "my/add_bankcard";
    }
    /**添加银行卡post*/
    @RequestMapping(value = "/user/add_bankcard", method = RequestMethod.POST)
    public String addUserBankcardPost(Model model,@Valid @ModelAttribute("UserBankcardModel") UserBankcardModel userBankcardModel, BindingResult result) {
        Subject currentUser = SecurityUtils.getSubject();
            //获取当前用户
            Session session = currentUser.getSession();
           CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
            String userId = currentUserModel.getUserId();
           User user=userService.getUserByUserId(userId);
            if (result.hasErrors()) {
            return "my/add_bankcard";
        }
        /**获取省名称*/
        BaseAreas provinceCode = baseAreasService.getAreaCodeByAreaName(userBankcardModel.getAddrProvinceCode());
        /**获取市名称*/
        BaseAreas cityCode = baseAreasService.getAreaCodeByAreaName(userBankcardModel.getAddrCityCode());
          UserBankcard userBankcard=new UserBankcard();
        userBankcard.setBankcardId(UUID.randomUUID().toString());
        userBankcard.setUserId(userId);
        userBankcard.setUserName(user.getUserName());
        userBankcard.setAccountName(userBankcardModel.getAccountName());//开户名
        userBankcard.setBank(userBankcardModel.getBank());//开户银行
        userBankcard.setBranch(userBankcardModel.getBranch());
        userBankcard.setAddrProvinceCode(userBankcardModel.getAddrProvinceCode());//开户省编码
        userBankcard.setAddrProvinceName(provinceCode.getAreaName());
        userBankcard.setAddrCityCode(userBankcardModel.getAddrCityCode());//开户市编码
        userBankcard.setAddrCityName(cityCode.getAreaName());
        userBankcard.setCardNo(userBankcardModel.getCardNo());//银行卡
        userBankcard.setCreationTime(new Date());
        int returnResult= userBankcardService.addBakcard(userBankcard);
        if (returnResult>0){
            model.addAttribute("error", "添加成功");
            return "redirect:/user/bankcard";
        } else model.addAttribute("error", "系统异常");
        return "redirect:/user/add_bankcard";
    }
}
